//
//  cCBaseLoginViewController.m
//  cyChat
//
//  Created by 楼楚 on 15/12/4.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "cCBaseLoginViewController.h"
#import "AppDelegate.h"
@implementation cCBaseLoginViewController
- (void)login
{
    //    登录
    /*
     官方的登录实现
     1 把密码和用户名放在cCUserInfo的单例
     2 调用AppDelegate的一个login 连接服务并登录
     
     */

   
    
    //隐藏键盘
    [self.view endEditing:YES];
    //登录之前给个提示
    [MBProgressHUD showMessage:@"正在登录中..... " toView:self.view ];
//    cCXMPPTool *app = [UIApplication sharedApplication].delegate;
//    app.registerOperation = NO;
    [cCXMPPTool sharedcCXMPPTool].registerOperation = NO;
    __weak typeof(self) selfVc = self;
    [[cCXMPPTool sharedcCXMPPTool] xmppUserLogin:^(XMPPResultType type){
        [selfVc handleResultType:type];
    }];
}
/*
 处理登录结果
 */
- (void)handleResultType:(XMPPResultType)type
{
    //主线程刷新UI
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view];
        switch (type) {
            case XMPPResultTypeLoginSuccess:
                NSLog(@"登录成功");
                [self enterMainPage];
                break;
            case XMPPResultTypeLoginFailue:
                NSLog(@"登录失败");
                [MBProgressHUD showError:@"用户名或密码不正确"toView:self.view ];
                break;
            case XMPPResultTypeNetErr:
                [MBProgressHUD showError:@"网络炸啦" toView:self.view ];
            default:
                break;
        }
    });
}
- (void) enterMainPage{
    //    更改用户的登录状态为yes
    [cCUserInfo sharedcCUserInfo].loginStatus = YES;
    
    //把用户登录成功的数据，保存到沙盒
    [[cCUserInfo sharedcCUserInfo]saveUserInfoToSanbox];
    //隐藏模态窗口
    [self dismissViewControllerAnimated:NO completion:nil];
    //登录成功来到主界面
    //登录成功后来到主界面
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    self.view.window.rootViewController = storyboard.instantiateInitialViewController;
    [UIStoryboard showInitialVCWithName:@"Main"];
}



@end
